package cn.gogoyc.pet.service.impl;

import cn.gogoyc.org.basic.servie.IBaiduAiAuditService;
import cn.gogoyc.org.basic.servie.impl.BaseServiceImpl;
import cn.gogoyc.pet.domain.Pet;
import cn.gogoyc.pet.domain.PetDetail;
import cn.gogoyc.pet.domain.PetOnlineAuditLog;
import cn.gogoyc.pet.domain.PetType;
import cn.gogoyc.pet.mapper.PetDetailMapper;
import cn.gogoyc.pet.mapper.PetMapper;
import cn.gogoyc.pet.mapper.PetOnlineAuditLogMapper;
import cn.gogoyc.pet.mapper.PetTypeMapper;
import cn.gogoyc.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 宠物业务实现类
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 类型无限级树
     *
     * @return
     */
    @Override
    public List<PetType> petTypeTree() {
        //查询所有
        List<PetType> petTypes = petTypeMapper.loadAll();
        //1.将所有部门信息保存在map中，方便根据id去获取部门信息，就不用查数据库了
        Map<Long, PetType> map = new HashMap<>();
        //遍历所有部门的集合
        for (PetType petType : petTypes) {
            map.put(petType.getId(), petType);
        }

        //空集合
        List<PetType> petTypeTree = new ArrayList<>();
        //再次遍历部门对象集合
        for (PetType petType : petTypes) {
            //parentid为null的肯定就是顶级部门
            if (petType.getParentId() == null) {
                //将顶级部门添加进集合
                petTypeTree.add(petType);
            } else {
                //有parentid那就是下级,获取他的上级部门id
                Long parentId = petType.getParentId();
                //去map中根据部门id查询上级部门对象
                PetType parentPetType = map.get(parentId);

                if (parentPetType != null) {
                    //去获取上级部门的部门树,再将子部门添加进对应的父部门的集合中
                    parentPetType.getChildren().add(petType);
                }
            }
        }
        return petTypes;
    }

    @Override
    public void onsale(List<Long> ids) {
        ids.forEach(aLong -> {
            Pet pet = petMapper.loadById(aLong);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            if (boo1 && boo2) {
                petMapper.updateOnsaleState(aLong, new Date());
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);
                log.setNote(aLong+"审核成功!");
                petOnlineAuditLogMapper.save(log);
            } else {
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(0);
                log.setNote(aLong+"审核失败!");
                petOnlineAuditLogMapper.save(log);
            }

        });
    }

    @Override
    public void offsale(List<Long> ids) {
        petMapper.updateOffState(ids, new Date());
    }

    @Transactional
    @Override
    public void save(Pet pet) {
        //添加宠物信息
        super.save(pet);
        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Transactional
    @Override
    public void remove(Long id) {
        petDetailMapper.removeByPetId(id);
        super.remove(id);
    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        ids.forEach(aLong -> remove(aLong));
    }
}
